package org.djw.pay.service.impl;

import org.djw.basic.exception.BusinessException;
import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.order.service.IProductOrderService;
import org.djw.pay.domain.AlipayInfo;
import org.djw.pay.domain.PayBill;
import org.djw.pay.mapper.AlipayInfoMapper;
import org.djw.pay.mapper.PayBillMapper;
import org.djw.pay.service.IPayBillService;
import org.djw.pay.utils.AliPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill billTmp) {
        if(billTmp == null){
            throw new BusinessException("使劲骂!!");
        }
        PayBill bill = payBillMapper.loadByUnionPaySn(billTmp.getUnionPaySn());
        if(bill == null){
            throw new BusinessException("使劲骂!!");
        }
        Long payChannel = bill.getPayChannel();
        switch (payChannel.intValue()) {// 0 支付宝 1 微信  2 银联
            case 0:{//支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AliPayUtils.pay(bill, info);
            }
            case 1:{//微信

            }
            case 2:{//银联

            }
            default: break;
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}